import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.regex.Pattern;

public class FileAnalyzer {
	private BufferedReader inputStream;
	private Vector<ReadingSet> readingSets = new Vector<ReadingSet>();
	public FileAnalyzer(File testFile){
		String lineRead = null;
		Vector<Double> row = new Vector<Double>();
		ReadingSet readingSet;
		
		//Set up all the readings.
		try {
			inputStream = new BufferedReader(new FileReader(testFile));

			//Remove headers.
			try{
				while(!Pattern.matches("^\\s*(Time|Wavelength).*", inputStream.readLine())){}
			}
			catch (NullPointerException e){
				System.out.println("Couldn't find where the headers end.");
			}

			//Read rest of document.
			lineRead = inputStream.readLine();
			while(lineRead!=null && !(Pattern.matches("^\\s*~End\\s*", lineRead))){
				
				//Identify the first row.
				StringTokenizer st = new StringTokenizer(lineRead);
				System.out.println(lineRead);
 				readingSet = new ReadingSet(st.nextToken(),Double.valueOf(st.nextToken()));
				row = new Vector<Double>();
				while (st.hasMoreTokens()) {
					row.add(Double.valueOf(st.nextToken()));
				}
				readingSet.addDataRow(row);
				
				//Add the rest of the rows...
				while(!Pattern.matches("^\\s*$", (lineRead = inputStream.readLine()))){	
					System.out.println(lineRead);
					st = new StringTokenizer(lineRead);
					row = new Vector<Double>();
					while (st.hasMoreTokens()) {
						row.add(Double.valueOf(st.nextToken()));
					}
					readingSet.addDataRow(row);
				}
				readingSets.add(readingSet);
				while(Pattern.matches("^\\s*$", (lineRead = inputStream.readLine()))){	}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Vector<ReadingSet> getReadingSets() {
		// TODO Auto-generated method stub
		return readingSets;
	}
}
